package com.ui.swing;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import com.util.SystemParams;

public class ParamsSettingDialog extends JPanel{
	
	public static final int SETUP_OK = 1;
	public static final int SETUP_CANCEL = 0;
	
	JDialog dialog;
	private int option;
	private SystemParams sysParams;
	
	/**------UI Component of this dialog------**/
    private JPanel hostPane = new JPanel();
    private JCheckBox chkboxHost = new JCheckBox();
    private JLabel lblHost = new JLabel();
    private JTextField txtHost = new JTextField();
    private JLabel lblPort = new JLabel();
    private JTextField txtPort = new JTextField();
    
    private JPanel othersPane = new JPanel();
    private JCheckBox chkboxRefresh = new JCheckBox();
    private JLabel lblTime = new JLabel();
    private JTextField txtTime = new JTextField();
    
    private JPanel ctrlPane = new JPanel();
    private JButton btnOK = new JButton();
    private JButton btnCancel = new JButton();    
    /**------End of UI Component of this dialog------**/
	
	public ParamsSettingDialog(SystemParams params) {
		option = SETUP_CANCEL;
		sysParams = params;
		
		initUI();
		installListeners();
		loadParams();
	}
	
	private void initUI() {          
        //Host pane
        chkboxHost.setText("Enable Host");
        lblHost.setText("Host:");
        txtHost.setText("");
        lblPort.setText("Port:");
        txtPort.setText("8080");
        
        hostPane.setBorder(new TitledBorder("Host Parameters"));
        hostPane.setLayout(new GridBagLayout());
        
        hostPane.add(chkboxHost, new GridBagConstraints(0, 0, 4, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE,
                new Insets(8, 8, 0, 8), 0, 0));
        hostPane.add(lblHost, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE,
                new Insets(8, 8, 8, 8), 0, 0));
        hostPane.add(lblPort, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE,
                new Insets(8, 8, 8, 8), 0, 0));
        hostPane.add(txtHost, new GridBagConstraints(1, 1, 1, 1, 0.8, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                new Insets(8, 8, 8, 8), 0, 0));
        hostPane.add(txtPort, new GridBagConstraints(3, 1, 1, 1, 0.2, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                new Insets(8, 8, 8, 8), 0, 0));
        
        //Others pane
        chkboxRefresh.setText("Auto Refresh");
        lblTime.setText("Time(S):");
        txtTime.setText("20");
        
        othersPane.setLayout(new GridBagLayout());
        othersPane.setBorder(new TitledBorder("Others"));
        
        othersPane.add(chkboxRefresh, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE,
                new Insets(8, 8, 0, 8), 0, 0));
        othersPane.add(lblTime, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE,
                new Insets(8, 8, 8, 8), 0, 0));
        othersPane.add(txtTime, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
                , GridBagConstraints.WEST, GridBagConstraints.NONE,
                new Insets(8, 8, 8, 8), 24, 0));
        
        
        //Control pane
        btnOK.setText("Save");
        btnCancel.setText("Cancel");
        ctrlPane.setLayout(new GridBagLayout());
        
        ctrlPane.add(btnOK, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
                , GridBagConstraints.EAST, GridBagConstraints.NONE,
                new Insets(8, 8, 8, 8), 0, 0));
        ctrlPane.add(btnCancel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
                , GridBagConstraints.CENTER, GridBagConstraints.NONE,
                new Insets(8, 8, 8, 8), 0, 0));
        
        
        //main panel
        this.setBorder(new TitledBorder("Preference Settings"));
        this.setLayout(new GridBagLayout());
        
        this.add(hostPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
                , GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));
        this.add(othersPane, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0
                , GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));        
        this.add(ctrlPane, new GridBagConstraints(0, 2, 1, 1, 0.0, 1.0
                , GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
                new Insets(0, 0, 0, 0), 0, 0));
	}
	
	private void installListeners() {
		chkboxHost.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.DESELECTED) {
					setHostSettingEnabled(false);
				} else {
					setHostSettingEnabled(true);
				}
			}
		});
		
		chkboxRefresh.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.DESELECTED) {
					setTimeSettingEnabled(false);
				} else {
					setTimeSettingEnabled(true);
				}
			}
		});
		
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				option = SETUP_OK;
				if(saveParams() == false) {
					JOptionPane.showMessageDialog(dialog, "Some parameters is invalid!");
					return;
				}
				
				if(dialog != null) {
					dialog.dispose();
					dialog = null;
				}
			}
		});
		
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				option = SETUP_CANCEL;
				dialog.dispose();
				dialog = null;
			}
		});
	}
	
	private void setHostSettingEnabled(boolean enabled) {
        lblHost.setEnabled(enabled);
        txtHost.setEnabled(enabled);
        lblPort.setEnabled(enabled);
        txtPort.setEnabled(enabled);
	}
	
	private void setTimeSettingEnabled(boolean enabled) {
		lblTime.setEnabled(enabled);
        txtTime.setEnabled(enabled);
	}
	
	private void loadParams() {
		chkboxHost.setSelected(sysParams.isUseProxy());
		setHostSettingEnabled(sysParams.isUseProxy());
		txtHost.setText(sysParams.getProxyHost());
		txtPort.setText(String.valueOf(sysParams.getPorxyPort()));
		
		chkboxRefresh.setSelected(sysParams.isAutoRefresh());
		setTimeSettingEnabled(sysParams.isAutoRefresh());
		txtTime.setText(String.valueOf(sysParams.getTimeForRefresh()));
	}
	
	private boolean saveParams() {
		boolean bRet = true;
		
		try {
			sysParams.setUseProxy(chkboxHost.isSelected());
			sysParams.setProxyHost(txtHost.getText().trim());
			int port = Integer.parseInt(txtPort.getText().trim());
			sysParams.setPorxyPort(port);
			
			sysParams.setAutoRefresh(chkboxRefresh.isSelected());
			long time = Long.parseLong(txtTime.getText().trim());
			sysParams.setTimeForRefresh(time);
		} catch(Exception exp) {
			bRet = false;
		}
		return bRet;
	}
	
	public SystemParams getSystemParams() {
		return sysParams;
	}
	
	public int showDialog(JFrame parent) {
		JDialog dlg = new JDialog(parent, "Preference:", true);
		dialog = dlg;
		dlg.setContentPane(this);
		dlg.setSize(400, 300);
		
		//set position to the center of parent
		Dimension d1 = dlg.getSize();
		Dimension d2 = parent.getSize();
		Point p = parent.getLocation();
		dlg.setLocation((d2.width-d1.width)/2+p.x, (d2.height-d1.height)/2+p.y);
		
		dlg.setVisible(true);		
		return option;
	}
}
